package com.example.mybatisplus.web.controller;

import java.util.ArrayList;
import java.util.List;

import com.example.mybatisplus.model.domain.ConRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.mapper.FirstAppointmentMapper;
import com.example.mybatisplus.model.domain.DutyTime;
import com.example.mybatisplus.model.domain.FirstAppointment;
import com.example.mybatisplus.model.domain.FirstViewAppDetail;
import com.example.mybatisplus.model.domain.StuInfo;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.FirstAppointmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


/**
 *
 *  前端控制器
 *
 *
 * @author cft
 * @since 2022-06-24
 * @version v1.0
 */
@Controller
@RequestMapping("/api/firstAppointment")
public class FirstAppointmentController {

    private final Logger logger = LoggerFactory.getLogger( FirstAppointmentController.class );

    @Autowired
    private FirstAppointmentService firstAppointmentService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        FirstAppointment  firstAppointment =  firstAppointmentService.getById(id);
        return JsonResponse.success(firstAppointment);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        firstAppointmentService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateFirstAppointment(FirstAppointment  firstAppointment) throws Exception {
        firstAppointmentService.updateById(firstAppointment);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建FirstAppointment
    *
    */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(@RequestBody FirstAppointment  firstAppointment) throws Exception {
        firstAppointment.recordTime();//记录提交时间
        if(firstAppointment.checkInfo()){ //检查是否填写完整
           return JsonResponse.success(null);
        }
        firstAppointmentService.save(firstAppointment);
        return JsonResponse.success(firstAppointment);
    }


	/* 
	* author:tqq
	* function:根据当前登录学生查询初访预约信息
	*/
    @GetMapping("/getFirstApoByStuId")
    @ResponseBody
	public JsonResponse showfirstapo() {
		List<FirstAppointment> list = firstAppointmentService.getBystuId(SessionUtils.getCurrentUserInfo().getUserId());
		return JsonResponse.success(list);
	}

	/* 
	 * author:cft
	 * function:查询所有待处理的学生初访预约信息
	 */
	@RequestMapping("/getAllWaitedFirstApply")
	@ResponseBody
	public JsonResponse getAllWaitedFirstApply(PageDTO pageDTO, FirstAppointment firstAppointment) {
		Page<FirstAppointment> list = firstAppointmentService.getAllWaitedFirstApply(pageDTO, firstAppointment);
		return JsonResponse.success(list);
	}
	


	@Autowired
	FirstAppointmentMapper firstAppointmentMapper;
	/* 
	 * author:cft
	 * function:查询待处理初访预约的信息、对应的学生信息以及学生初访的结果
	 */
	@RequestMapping("/getAllWaitedFirstAppInfo")
	@ResponseBody
	public JsonResponse getAllWaitedFirstAppInfo(PageDTO pagedto, FirstAppointment firstAppointment, StuInfo stu) {
		PageHelper.startPage(pagedto.getPageNo(), pagedto.getPageSize());
		List<FirstViewAppDetail> list = firstAppointmentMapper.getAllWaitedFirstAppInfo(firstAppointment,
				stu);
		PageInfo<FirstViewAppDetail> pageInfo = new PageInfo<>(list);
		return JsonResponse.success(pageInfo);
	}


    /*
    * author:tqq
    * 学生撤回初访预约
    * */
    @RequestMapping("/revocateFirstApo")
    @ResponseBody
    public JsonResponse revocate(@RequestBody FirstAppointment firstAppointment){
        firstAppointmentService.updatewithCurrentUser(firstAppointment);
        return JsonResponse.success("成功撤回");
    }

    /*
    * author:tqq
    * 根据结案报告数组中的 id查询初访预约记录
    * */
    @PostMapping("/getById")
    @ResponseBody
    public JsonResponse getById(@RequestBody ConRecord conRecords[]){
        System.out.println("hhhhhhhhhh"+conRecords);
        FirstAppointment []firstAppointments = new FirstAppointment[conRecords.length];
        for (int i = 0; i < conRecords.length; i++) {
            firstAppointments[i] = firstAppointmentService.getByoneStuId(conRecords[i].getConStuId());
        }
        return JsonResponse.success(firstAppointments);
    }


	/*
	 * author:cft
	 * function:获得学生选择的所有初访时间
	 */
	@RequestMapping("/getStuApoTime")
	@ResponseBody
	public JsonResponse getStuApoTime(@RequestBody JSONObject jsonObject) {
		String time1 = jsonObject.getString("selectedPeriod1");
		String time2 = jsonObject.getString("selectedPeriod2");
		String time3 = jsonObject.getString("selectedPeriod3");
		String[] time = new String[] { time1, time2, time3 };
		String[] list = firstAppointmentService.getStuApoTime(time);
		return JsonResponse.success(list);
	}

	/*
	 * author:cft
	 * function:修改学生初访预约状态
	 */
	@RequestMapping("/changeFApoStatus")
	@ResponseBody
	public JsonResponse changeFApoStatus(@RequestBody JSONObject jsonObject) {
		System.out.println(jsonObject);
		FirstAppointment firstAppointment = new FirstAppointment();
		firstAppointment.setFApoId(Long.parseLong(jsonObject.getString("fApoId")))
				.setFApoResult(jsonObject.getString("fApoResult")).setApoStatus(jsonObject.getString("apoStatus"));
		firstAppointmentService.updateById(firstAppointment);
		return JsonResponse.success("success");
	}

	/*
	 * author:cft
	 * function:得到已经安排过的初访预约的信息
	 */
	@RequestMapping("/getAllArrangedFirstApo")
	@ResponseBody
	public JsonResponse getAllArrangedFirstApo(PageDTO pagedto, FirstAppointment firstAppointment, StuInfo stu) {
		PageHelper.startPage(pagedto.getPageNo(), pagedto.getPageSize());
		List<FirstViewAppDetail> list = firstAppointmentMapper.getAllArrangedFirstAppInfo(firstAppointment,
				stu);
		PageInfo<FirstViewAppDetail> pageInfo = new PageInfo<>(list);
		return JsonResponse.success(pageInfo);
	}
}

